package cn.ccit.DAOImp;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.ccit.DAO.UserDAO;
import cn.ccit.entites.User;
import cn.ccit.util.MyDBUtil;

public class UserDAOimp implements UserDAO {
	private Session session;
	private static final Log log = LogFactory.getLog(UserDAOimp.class);
	public UserDAOimp() {
		
	}
	
	@Override
	public boolean regUser(User user) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.save(user);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();//深层次的输出调用异常流程
				log.error("save user fail", e);//日志信息
				MyDBUtil.rollBack();
				return false;
			}finally{
				MyDBUtil.closeSession();
			}
			
		}
		return false;
	}

	@Override
	public User loginUser(User user) {
		// 使用手机号和密码登陆
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from User as user where user.phone=? and user.password=?";
				Query query=session.createQuery(hql);
				query.setString(0, user.getPhone());
				query.setString(1, user.getPassword());
				List list=query.list();
				session.beginTransaction().commit();
				if(list.size()>0){
					User u =(User) list.get(0);
					return u;
				}
				
				
			} catch (Exception e) {
				
				e.printStackTrace();
				log.error("not found user",e);
				MyDBUtil.rollBack();
				
			}finally{
				MyDBUtil.closeSession();
			}
		}
		return null;
	}

	@Override
	public boolean changePwd(User user) {
		
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
		Query query = session.createQuery("update User u set u.password = '"+user.getPassword()+"' where id ="+user.getId());
				int state=query.executeUpdate();
				session.beginTransaction().commit();
				if(state>0){
					return true;
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("update user is fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		
		return false;
	}

	@Override
	public boolean changeUserInfo(User user) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.update(user);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				
				e.printStackTrace();
				log.error("update user is fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public List<User> getSomeUsers(int startId, int limit) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from User as user order by id desc";
				Query query=session.createQuery(hql);
				Long count=(Long)session.createQuery("select count(*) from User").uniqueResult();
				if(count>limit){
					query.setFirstResult(startId);
					query.setMaxResults(limit);
				}
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("getSomeUsers fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	@Override
	public User searchUserInfoByID(int id) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from User as user where user.id=?";
				Query query=session.createQuery(hql);
				query.setInteger(0, id);
				User user=(User)query.list().get(0);
				session.beginTransaction().commit();
				return user;
			} catch (Exception e) {
				
				e.printStackTrace();
				log.error("fail to user by id", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public List<User> searchUserInfoByName(String name) {
		
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from User as user where user.name=?";
				Query query=session.createQuery(hql);
				query.setString(0, name);
				System.out.println(name);
				List list=query.list();
				System.out.println(list.size());
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				
				e.printStackTrace();
				log.error("search the user by name", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public boolean removeUser(User user) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.delete(user);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();//深层次的输出调用异常流程
				log.error("del user fail", e);//日志信息
				MyDBUtil.rollBack();
				return false;
			}finally{
				MyDBUtil.closeSession();
			}
			
		}
		return false;
	}

}
